<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="60%" append-to-body custom-class="import-dialog">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="用户名称" prop="userName">
          <el-input v-model="queryParams.userName" placeholder="请输入用户名称" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['sys-enterprise:account:add']">新增</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" ref="table">
        <el-table-column label="用户名称" align="center" prop="userName" />
        <el-table-column label="手机号" align="center" prop="phonenumber" />
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <span>{{ scope.row.status == 0 ? '启用' : '禁用' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <!-- TODO 添加按钮权限 -->
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)"
              v-hasPermi="['sys-enterprise:account:edit']">编辑</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handlePassword(scope.row)"
              v-hasPermi="['sys-enterprise:account:password']">修改密码</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['sys-enterprise:account:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </el-dialog>
    <edit v-model="addVisible" :title="editTitle" :data="form" @success="getList" />
    <password v-model="passwordVisible" :userId="userId" />
  </div>
</template>

<script>
import { pageByDep, deleteUserBusiness,getUserBusiness } from "@/api/sys-enterprise/account";
import edit from './edit.vue'
// import password from './password.vue';
import password from "@/views/sys-user/enterprise-user/password.vue";
export default {
  props: {
    title: {
      default: "账号信息",
    },
    value: {
      default: false,
    },
    tenantId: {
      default: undefined
    }
  },
  dicts: ["department_type"],
  components: {
    edit,
    password
  },
  data() {
    return {
      visible: false,
      addVisible: false,// 显示 添加/编辑
      passwordVisible: false,//显示 修改密码
      editTitle: '',
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: undefined,
      },
      form: {},
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      //列表
      list: [],
      //用户id
      userId: undefined,
    };
  },
  watch: {
    value(newVal, oldVal) {
      if (newVal) {
        this.init();
      }
      this.visible = newVal; // 监测value的变化，并赋值。
    },
    visible(val) {
      this.$emit("input", val); // 此处监测visible目的为关闭弹窗时，重新更换value值，注意emit的事件一定要为input。
    },
  },
  methods: {
    init() {
      this.getList()
      this.reset()
    },
    reset() {
      // this.dateRange = [];
      this.form = {}
      this.resetForm("form");
    },
    handleSelectionChange() {

    },
    /** 查询岗位列表 */
    getList() {
      this.queryParams.tenantId = this.tenantId
      this.loading = true;
      pageByDep(this.queryParams)
        .then((response) => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAdd() {
      this.form = {
        userName: '',
        phonenumber: undefined,
        roleId: undefined,
        status: '0',
        tenantId: this.tenantId
      }
      this.editTitle = '新增账号'
      this.addVisible = true
    },
    //编辑
    handleEdit(row) {
      let params = {
        userId:row.userId,
        tenantId:this.tenantId
      }
      console.log(params);
      getUserBusiness(params).then(response => {
        this.form = response.data
        this.form.tenantId = this.tenantId
        this.editTitle = '编辑账号'
        this.addVisible = true
      })

    },
    //修改密码
    handlePassword(row) {
      this.userId = row.userId
      this.passwordVisible = true
    },
    handleDelete(row) {
      let data = {
        tenantId: this.tenantId,
        userId: row.userId
      }
      this.$modal.confirm('确定删除数据？').then(function () {
        return deleteUserBusiness(data);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    }
  },
};
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/dialog.scss";
</style>
